package com.letwal.cmcvnd.earn.todayEarn

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.letwal.cmcvnd.adata.home.getInviteFriendEarn.ReqGetInviteFriendEarn
import com.letwal.cmcvnd.aviewmodel.HomeViewModel
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.databinding.ActivityTotalEarnDetailWithdrawalTimesBinding
import com.letwal.cmcvnd.earn.todayEarn.adapter.TotalEarnDetailWithdrawalTimesAdapter
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*
import com.letwal.cmcvnd.home.repo.HomeCoinManager

/** 我的币页 */
@SuppressLint("CustomSplashScreen")
class TotalEarnDetailWithdrawalTimesActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityTotalEarnDetailWithdrawalTimesBinding

    //适配器
    private lateinit var mTotalEarnDetailWithdrawalTimesAdapter: TotalEarnDetailWithdrawalTimesAdapter

    //home viewModel
    private val mHomeViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mHomeViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.my_wallet.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityTotalEarnDetailWithdrawalTimesBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //初始化刷新组件
            initRefreshUI()

            //监听返回
            onHandleLiveDataCallback()

            //初始化适配器
            mHomeViewModel.mGetTransferInExtractList.clear()
            mTotalEarnDetailWithdrawalTimesAdapter = TotalEarnDetailWithdrawalTimesAdapter(mHomeViewModel.mGetTransferInExtractList)

            mActivityBinding.listTotalEarnDetailWithdrawalTimes.layoutManager = LinearLayoutManager(this)
            mActivityBinding.listTotalEarnDetailWithdrawalTimes.adapter = mTotalEarnDetailWithdrawalTimesAdapter

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        //获取转入收益详情
        getTransferInExtractList()
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    private fun onHandleLiveDataCallback() {
        mHomeViewModel.onGetTransferInExtractListLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mHomeViewModel.mGetTransferInExtractList.clear()
                    mHomeViewModel.mGetTransferInExtractList.addAll(result)
                    mTotalEarnDetailWithdrawalTimesAdapter.notifyDataSetChanged()

                    if (mHomeViewModel.mGetTransferInEarnList.size > 0) {
                        mActivityBinding.emptyLayout.llEmptyList.visibility = View.GONE
                    } else {
                        mActivityBinding.emptyLayout.llEmptyList.visibility = View.VISIBLE
                    }
                }
            }
        }
    }


    /** 获取转入收益详情 */
    private fun getTransferInExtractList() {
        HomeCoinManager.mGlobalCoinTypeLiveData.value?.let {
            //获取转入收益详情
            val reqGetInviteFriendEarn = ReqGetInviteFriendEarn(coinType = it)
            reqGetInviteFriendEarn.showLoading = false
            mHomeViewModel.getTransferInExtractList(reqGetInviteFriendEarn)
        }
    }

    /** 初始化下拉刷新 */
    private fun initRefreshUI() {
        connectSwipeRefreshLayout(mActivityBinding.swipeRefresh) {
            //获取转入收益详情
            getTransferInExtractList()
        }
    }
}